require 'open-uri'
require 'rubygems'
require 'nokogiri'

#Call requires here
#Requires to classes, modules, controllers, routers, and so. 


class Model 
	
	def initialize()
		@keysToAccess = []
		@auxKeysToAccess = []
		@candidateTotal = []
		@names = []
		@xmlBuffer = []
		@regionNameList = []
		@subscriptor = nil;
	end
	# RevisarHojasFinales: Gone
	# mapTotalVotes : Gone
	
	def addSubscriptor(viewsub)
		@subscriptor = viewsub
	end
	
	# returns an array containing the keys to be processed from read -filename-	
	def collectData(filename)
		doc = Nokogiri::XML(open("http://spreadsheets.google.com/feeds/cells/"+filename+"/od6/public/basic"))
		keys = Array.new
		i = 2
		j = 0
		while i+1 < doc.css('content').size
			keys[j] = (doc.css('content')[i+1].text)
			j+=1
			i+=2
		end	
		index = 0
		while index < keys.size
			keys = keys | collectSubData(keys[index])		
			index+=1
		end		
		return keys
	end

	# complementary to collectData. Adds keys to collectData keys array from read -filename-
	def collectSubData(filename)
		doc = Nokogiri::XML(open("http://spreadsheets.google.com/feeds/cells/"+filename+"/od6/public/basic"))
		subKeys = Array.new
		i = 2
		j = 0
		while i+1 < doc.css('content').size		
			if doc.at_css('content').text != "Mesa"
				subKeys[j] = (doc.css('content')[i+1].text)
			else
				subKeys.clear
			end
			j+=1
			i+=2
		end
		return subKeys
	end

	# receives keys array and returns a hash with a summary of the votes retrieved from the keys array -key-
	def dataProcess(key)
		#puts key.size
		#tableKey = case key.size		
			#when 44 then key[21,24]             QUE QUEDE ESTO COMO EVIDENCIA DE 
			#when 6 then key[0,6]                UN SUCIO INTENTO DE HACK POR PARTE
			#else fail "Unknown"                 DEL BADGER TEAM. GG 
		#end                                   P.D -> se la ganaron, yo les dije Jruby, no ruby corriente.
		names = Array.new
		votes = Array.new
		voteHash = Hash.new
		key.each{|node|
			doc = Nokogiri::XML(open("http://spreadsheets.google.com/feeds/cells/"+node+"/od6/public/basic"))		
			j = 2
			k = 0
			while j+1 < doc.css('content').size		
				if names.empty?
					names[k] = doc.css('content')[j].text
					votes[k] = doc.css('content')[j+1].text.to_i
				else
					if names.include?(doc.css('content')[j].text)
						votes[names.index(doc.css('content')[j].text)] = votes[names.index(doc.css('content')[j].text)].to_i + doc.css('content')[j+1].text.to_i
					else
						names[k] = doc.css('content')[j].text
						votes[k] = doc.css('content')[j+1].text.to_i
					end
				end
				k+=1
				j+=2
			end
		}	
		toHash = 0
		while toHash < names.size
			voteHash[names[toHash]] = votes[toHash]
			toHash += 1
		end	
		return voteHash
	end

	# separates contained keys in read -filename- and processes one by one to return a hash array where each element is a hash with the votes
	# summary from read -filename-
	def getRegionVotes(regions)	
		doc = Nokogiri::XML(open("http://spreadsheets.google.com/feeds/cells/"+filename+"/od6/public/basic"))
		regions = Array.new
		regionVotes = Array.new
		index = 2
		ind = 0
		while index+1 < doc.css('content').size
		regions << doc.css('content')[index+1].text
			index+=2
		end	
		regions.each{|node|
			sub = dataProcess(collectData(node))
			regionVotes << sub		
		}
		return regionVotes
	end
end
